//
//  GoodsInformationViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/17.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GoodsInformationViewController.h"
#import "GoodsInforViewModel.h"
#import "StoreInformationViewController.h"
#import "StellCaiculatorViewController.h"
#import "BaoJiaViewController.h"
#import "ShopCarViewController.h"

@interface GoodsInformationViewController ()<GoodsInforDelegate>{
    BOOL isHider;
    //
    CGFloat gunHiderTop;
    //
    UIButton *popBut;
    //
    UIButton * rightShouCanBut;
    UIButton * rightShareBut;
}
@property (nonatomic,strong) GoodsInforViewModel * goodsInforViewModel;
@end

@implementation GoodsInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isHider = YES;
    gunHiderTop = 0;
    self.goodsInforViewModel = [[GoodsInforViewModel alloc] initWithViewToViewModel:self.groupedTableView theSuperView:self.view];
    self.goodsInforViewModel.isSelect = self.is_GoodsPush;
    self.goodsInforViewModel.g_id = self.goods_id;
    self.goodsInforViewModel.delegate = self;
    [self addNavigationItemWithImageNames:@[@"goods_share",@"goods_shoucan"] isLeft:NO target:self action:@selector(rightAction:) tags:@[@200,@201]];
    UIView *barBackgroundView = self.navigationController.navigationBar.subviews[0];
    [barBackgroundView addObserver:self
                     forKeyPath:@"alpha"
                        options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                        context:nil];
    NSMutableArray * rightItems = [NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<2; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 40, 40);
        btn.tag = i+200;
        if (i==0) {
            rightShareBut = btn;
            [btn setImage:[UIImage imageNamed:@"icon_fenxiang_beijing"] forState:UIControlStateNormal];
        }else{
            rightShouCanBut = btn;
            [btn setImage:[UIImage imageNamed:@"icon_shoucang_beijing"] forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [rightItems addObject:item];
    }
    self.navigationItem.rightBarButtonItems = rightItems;
}

-(void)rightAction:(UIButton *)sender{
    
}

-(UIBarButtonItem *)rt_customBackItemWithTarget:(id)target action:(SEL)action{
    popBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [popBut setImage:[UIImage imageNamed:@"icon_fanhui_beijing"] forState:UIControlStateNormal];
    popBut.frame = CGRectMake(0, 0, 40, 40);
    [popBut setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    [popBut addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:popBut];
}
#pragma mark 监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"alpha"]){
        NSLog(@"%@",[object valueForKey:@"alpha"]);
        NSLog(@"ChangeInfo:%@",change);
        CGFloat alphaNum = [[object valueForKey:@"alpha"] floatValue];
        UIView *barBackgroundView = self.navigationController.navigationBar.subviews[0];// _UIBarBackground
        if (isHider && alphaNum == 1) {
            barBackgroundView.alpha = 0;
            isHider = NO;
            self.title = @"";
            [popBut setImage:[UIImage imageNamed:@"icon_fanhui_beijing"] forState:UIControlStateNormal];
            [rightShareBut setImage:[UIImage imageNamed:@"icon_fenxiang_beijing"] forState:UIControlStateNormal];
            [rightShouCanBut setImage:[UIImage imageNamed:@"icon_shoucang_beijing"] forState:UIControlStateNormal];
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (gunHiderTop == 1) {
        isHider = NO;
    }
    DLog(@"viewWillDisappear");
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (gunHiderTop == 0) {
        isHider = YES;
    }
    DLog(@"viewWillAppear");
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    DLog(@"viewDidAppear");
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    DLog(@"viewDidDisappear");
}

#pragma marek set
-(void)setIs_GoodsPush:(BOOL)is_GoodsPush{
    _is_GoodsPush = is_GoodsPush;
}

-(void)setGoods_id:(NSInteger)goods_id{
    _goods_id = goods_id;
}
#pragma mark --- GoodsInforDelegate
-(void)scrollViewDidScroll_y:(CGFloat)sys_y{
    UIView *barBackgroundView = self.navigationController.navigationBar.subviews[0];// _UIBarBackground
    if (sys_y > 160) {
        barBackgroundView.alpha = 1;
        gunHiderTop = 1;
        self.title = @"商品详情";
        [popBut setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
        [rightShareBut setImage:[UIImage imageNamed:@"icon_fenxiang"] forState:UIControlStateNormal];
        [rightShouCanBut setImage:[UIImage imageNamed:@"icon_shoucang"] forState:UIControlStateNormal];
        
    }else{
        barBackgroundView.alpha = 0;
        gunHiderTop = 0;///
        self.title = @"";
        [popBut setImage:[UIImage imageNamed:@"icon_fanhui_beijing"] forState:UIControlStateNormal];
        [rightShareBut setImage:[UIImage imageNamed:@"icon_fenxiang_beijing"] forState:UIControlStateNormal];
        [rightShouCanBut setImage:[UIImage imageNamed:@"icon_shoucang_beijing"] forState:UIControlStateNormal];
    }
}

-(void)buyNowGoods{
    
}

-(void)pushGoodsStore:(HightQualityStoreModel_Infos *)storeModel{
    isHider = YES;
    StoreInformationViewController * storeVC = [StoreInformationViewController new];
    storeVC.qua_Model = storeModel;
    [self.navigationController pushViewController:storeVC animated:YES];
}

-(void)pushCalculator{
    isHider = YES;
    StellCaiculatorViewController * stellCaiculatorVC = [StellCaiculatorViewController new];
    [self.navigationController pushViewController:stellCaiculatorVC animated:YES];
}

-(void)selectGoodsAction{
    for (UIViewController *vc in self.rt_navigationController.rt_viewControllers) {
        if ([vc isKindOfClass:[BaoJiaViewController class]]) {
            BaoJiaViewController *mainVC = (BaoJiaViewController *)vc;
            [self.navigationController popToViewController:mainVC animated:YES];
        }
    }
}

-(void)pushShopCar{
    ShopCarViewController * shopCarVC = [ShopCarViewController new];
    [self.navigationController pushViewController:shopCarVC animated:YES];
}

-(void)dealloc{
    UIView *barBackgroundView = self.navigationController.navigationBar.subviews[0];
    [barBackgroundView removeObserver:self forKeyPath:@"alpha"];
}

@end
